<?php $this->fetch('public/head') ?>
<div class="layui-tab layui-tab-brief">
    <ul class="layui-tab-title">
        <li class="layui-this">成员列表</li>
    </ul>
    <div class="layui-tab-content">
        <div id="ss" class="layui-form layui-form-pane">
            <div class="layui-inline">
                <label class="layui-form-label">部门</label>
                <div class="layui-input-inline">
                    <select name="depart_id" lay-filter="query" class="query">
                        <?php foreach($department as $k=>$v): ?>
                            <option value="<?php echo $v['id'] ?>" <?php if(input('depart_id')==$k){echo 'selected="selected"';} ?>><?php echo str_repeat('—', $v['level']-1).$v['name'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>
        <hr>

        <div class="layui-tab-item layui-show">
            <table id="dataList" lay-filter="dataList" class="layui-hide"></table>
        </div>
    </div>
</div>
<?php $this->fetch('public/script') ?>

<script>
    layui.use(['table'], function(){
        var table = layui.table
            ,form = layui.form;

        getLists();
        form.on('select(query)', function(data){
            $(data.elem).trigger('change',data.elem); //此处可让select支持change事件，trigger方法触发被选元素的指定事件类型。
        });
        //改变查询条件后重新获取列表数据
        $('.query').change(function () {
            getLists();
        });

        function getLists() {
            let params = HS.getParams('.query');
            //数据表格
            let load_table = layer.load(0, {time: 30000});
            table.render({
                elem: '#dataList'
                , height:$(window).height()-155
                , url: '<?php echo url('getLists') ?>' //数据接口
                , method: 'post'
                , where: params
                , response: {
                    countName: 'total' //数据总数的字段名称，默认：count
                }
                , page: {
                    limit: 20
                    , limits: [20, 30, 50, 100]
                    , groups: 5
                    , layout: [ 'refresh']
                } //开启分页
                , toolbar: '<div><span class="fl">已选 <i class="checkedNum"></i> 条信息</span> ' +
                    '<a class="layui-btn layui-btn-danger layui-btn-sm checkbox_delete">删除</a></div>'
                , defaultToolbar: ['filter']    //, 'print', 'exports'
                , cols: [[ //表头
                    {type: 'checkbox', width: 40}
                    ,{field: 'userid', title: 'ID',  event: 'detail'}
                    ,{field: 'name', title: '姓名',  event: 'detail'}
                    , {field: 'position', title: '职位', width: '35%', event: 'detail'}
                    , {field: 'mobile', title: '电话', width: '35%', event: 'detail'}
                    // , {field: 'is_show', title: '当前状态', templet: function (d) {
                    //         return '<div><input type="checkbox" name="is_show" value="'+d.id+'" lay-skin="switch" lay-text="显示|隐藏" lay-filter="checkbox" '+(d.is_show==1?'checked':'')+'></div>';
                    //     }, event: 'detail'}
                    // , {field: 'is_top', title: '置顶', templet: function (d) {
                    //         return '<div><input type="checkbox" name="is_top" value="'+d.id+'" lay-skin="switch" lay-text="是|否" lay-filter="checkbox" '+(d.is_top==1?'checked':'')+'></div>';
                    //     }, event: 'detail'}
                    // , {field: 'add_time', title: '添加时间', event: 'detail'}
                    // , {title: '操作', width: '10%', templet: function (d) {
                    //         return '<div><a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>'+
                    //             '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a></div>';
                    //     }, event: 'detail'}
                ]]
                , text: {none: '暂无相关数据'}
                , done: function (res, curr, count) {
                    layer.close(load_table);
                    $('.checkedNum').text('0');//刷新后清除选择数目
                    $('.checkbox_delete').unbind("click").click(function () {   //多选删除
                        let checkStatus = table.checkStatus('dataList')
                            ,data = checkStatus.data//包含行信息的data二维
                            ,ids=[];
                        let num = data.length;
                        if(num === 0)return layer.msg('请选择条目');
                        $.each(data,function (k, v) {
                            ids.push(v.id);
                        });
                        layer.confirm('确定删除所选信息吗', {icon: 3, title:'提示'}, function(index){
                            let loading = layer.load(1);
                            $.post('<?php echo url('delete') ?>',{ids:ids},function (res) {
                                layer.close(loading);layer.msg(res.msg);
                                if(res.code === 1){
                                    layer.close(index);
                                    table.reload('dataList',{page: {curr: 1 }});//多条删除后表格重载,//重新从第 1 页开始
                                }
                            });
                        });
                    })
                }
            })
        }
        //监听复选框选择
        table.on('checkbox(dataList)', function(obj){
            let checkStatus = table.checkStatus('dataList')
                ,data = checkStatus.data;//包含行信息的data二维
            $('.checkedNum').text(data.length)
        });
        //监听工具条
        table.on('tool(dataList)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            //var tr = obj.tr; //获得当前行 tr 的DOM对象
            if(layEvent === 'edit'){ //编辑
                location.href = HS.U('<?php echo url('edit',['pid'=>input('pid')]) ?>',{id:data.id});
            } else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除该信息吗', {icon: 3, title:'提示'}, function(index){
                    let loading = layer.load(1);
                    $.post('<?php echo url('delete') ?>',{id:data.id},function (data) {
                        layer.close(loading);layer.msg(data.msg);
                        if(data.code === 1){
                            layer.close(index);obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        }
                    });
                });
            }
        });
        //显示/隐藏、置顶操作
        form.on('switch(checkbox)', function(obj){
            let index = layer.load(1),param={};
            param[this.name] = obj.elem.checked?1:0;param.id = this.value;
            $.post('<?php echo url('flag') ?>',param,function (data) {
                layer.close(index);layer.msg(data.msg);
                if(data.code === 0){
                    obj.elem.checked = !obj.elem.checked;form.render('checkbox');
                }
            });
        });
    });
</script>